package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.pojo.Visitors;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @Author: ymm
 * @Description: 类内容说明
 * @Date: Created in 16:22 2021/1/13
 * @ModifiedBy:
 */
@Service(version = "1.0.0")
public class VisitorsApiImpl implements VisitorsApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     *保存来访记录
     * @param visitors
     * @return
     */
    @Override
    public String saveVisitor(Visitors visitors) {
        visitors.setId(ObjectId.get());
        visitors.setDate(System.currentTimeMillis());

        this.mongoTemplate.save(visitors);
        return visitors.getId().toHexString();
    }

    /**
     *按照数量进行排序,查询最近的访客信息
     * @param userId
     * @param num
     * @return
     */
    @Override
    public List<Visitors> topVisitor(Long userId, Integer num) {
        Pageable pageable = PageRequest.of(0,num, Sort.by(Sort.Order.desc("date")));
        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageable);

        return this.queryVisitorList(query);
    }


    /**
     *按照时间倒序排序，查询最近的访客信息
     * @param userId
     * @param date
     * @return
     */
    @Override
    public List<Visitors> topVisitor(Long userId, Long date) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                        .and("date").lt(date));
        return this.queryVisitorList(query);
    }

    @Override
    public List<Visitors> topVisitor(Long userId, Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("date")));
        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageable);
        return this.queryVisitorList(query);
    }


    /**
     * 抽取出的公共方法,查询访客列表
     * @param query
     * @return
     */
    private List<Visitors> queryVisitorList(Query query) {
        //1.通过mongodb查询访客信息
        List<Visitors> visitorsList = this.mongoTemplate.find(query,Visitors.class);

        //2.遍历查询到的访客集合,查询得分
        for (Visitors visitors : visitorsList) {
            Query queryRecommend = Query.query(Criteria.where("toUserId").is(visitors.getUserId())
                                    .and("userId").is(visitors.getVisitorUserId()));

            //3.查询出推荐的用户
            RecommendUser recommendUser = this.mongoTemplate.findOne(queryRecommend,RecommendUser.class);

            //4.根据推荐用户,获取得分
            if (null != recommendUser){
                visitors.setScore(recommendUser.getScore());
            }else {
                visitors.setScore(30d);
            }
        }

        //5.返回访客列表
        return visitorsList;
    }
}
